package com.wiretap.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wiretap.dao.ContactDao;
import com.wiretap.po.Contact;
import com.wiretap.util.Pager;

public class ContactDaoImpl implements ContactDao {

	private SessionFactory sessionFactory;

	@Override
	public void addContact(Contact contact) throws Exception {
		this.sessionFactory.getCurrentSession().persist(contact);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<Contact> getAllContactByPhone(String ownerPhone, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			int totalCount = sesion.createQuery("from Contact c where c.ownerPhone=:ownerPhone").setString("ownerPhone", ownerPhone).list().size();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<Contact> list = sesion.createQuery("from Contact c where c.ownerPhone=:ownerPhone order by c.addTimestamp desc").setString("ownerPhone", ownerPhone)
					.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<Contact> pager = new Pager<Contact>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getAllContactByPhone error");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Contact getContact(String ownerPhone, String number) throws Exception {
		try {
			Session session = this.sessionFactory.getCurrentSession();
			List<Contact> resultList = session.createQuery("from Contact c where c.ownerPhone=:ownerPhone and c.number=:number").setString("ownerPhone", ownerPhone).setString("number", number).list();
			if (resultList != null && !resultList.isEmpty()) {
				return resultList.get(0);
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getContact error");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addContact(String ownerPhone, List<Contact> contacts) throws Exception {
		if (contacts == null || contacts.isEmpty()) {
			return;
		}
		try {
			StringBuffer sqlBuffer = new StringBuffer("insert ignore into wiretap_contact(id,name,number,ownerPhone,addDate,addTimestamp) values ");
			int size = contacts.size();
			for (int i = 0; i < size; i++) {
				sqlBuffer.append("(?,?,?,?,?,?)");
				if (i != size - 1) {
					sqlBuffer.append(",");
				}
			}
			SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sqlBuffer.toString());
			for (int i = 0; i < size; i++) {
				Contact c = contacts.get(i);
				String number = c.getNumber();
				query.setString(6 * i + 0, ownerPhone + number);
				query.setString(6 * i + 1, c.getName());
				query.setString(6 * i + 2, number);
				query.setString(6 * i + 3, ownerPhone);
				query.setDate(6 * i + 4, new Date());
				query.setLong(6 * i + 5, System.currentTimeMillis());
			}
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("addContact发生异常");
		}
	}

	@Override
	public int deleteContactByOwnerPhone(String ownerPhone) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Contact c where c.ownerPhone=:ownerPhone").setString("ownerPhone", ownerPhone).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteContactByOwnerPhone发生异常");
		}
	}
}
